﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DAO;
namespace FDeal.WUC
{
    public partial class WUCComment : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadComment();
            }
        }
        private void LoadComment()
        {
            CommentBUS cb = new CommentBUS();
            gvTinKM.DataSource = cb.LietKe();
            gvTinKM.DataBind();
        }
        public static int idong;
        public static String idctd;

        protected void gvTinKM_RowUpdating1(object sender, GridViewUpdateEventArgs e)
        {
            if (e.NewValues == e.OldValues)
                e.Cancel = true;
            else
            {
                CommentBUS ctBUS = new CommentBUS();
                String matin = gvTinKM.DataKeys[idong].Value.ToString();
                COMMENT chitiet = ctBUS.ChiTiet(matin);

                chitiet.IDCOMMENT = ((TextBox)gvTinKM.Rows[e.RowIndex].Cells[0].FindControl("TextBox1")).Text.ToString();
                chitiet.IDTAIKHOAN = ((TextBox)gvTinKM.Rows[e.RowIndex].Cells[1].FindControl("TextBox2")).Text.ToString();
                chitiet.NOIDUNG = ((TextBox)gvTinKM.Rows[e.RowIndex].Cells[3].FindControl("TextBox3")).Text.ToString();
                chitiet.NGAY = DateTime.Parse(((TextBox)gvTinKM.Rows[e.RowIndex].Cells[3].FindControl("TextBox4")).Text.ToString());
                //String tinhtrang = ((TextBox)gvTinKM.Rows[e.RowIndex].Cells[5].FindControl("TextBox7")).Text.ToString();
                ctBUS.Them(chitiet);
                gvTinKM.EditIndex = -1;
                LoadComment();
            }
        }

        protected void gvTinKM_RowEditing1(object sender, GridViewEditEventArgs e)
        {
            idong = e.NewEditIndex;

            idctd = gvTinKM.DataKeys[idong].Value.ToString();
            gvTinKM.EditIndex = idong;

            LoadComment();
        }

        protected void gvTinKM_RowCancelingEdit1(object sender, GridViewCancelEditEventArgs e)
        {
            gvTinKM.EditIndex = -1;
            LoadComment();
        }

        protected void gvTinKM_RowDeleting1(object sender, GridViewDeleteEventArgs e)
        {
            CommentBUS ctBUS = new CommentBUS();
            COMMENT cn = ctBUS.ChiTiet(idctd);
            ctBUS.Xoa(cn);
            gvTinKM.EditIndex = -1;
            LoadComment();
        }
    }
}